package indi.xliuovo.coursedesign.musicplayer;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with Android Studio
 *
 * @Author xliuOvO
 */
public class QuestionFragment extends Fragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    View questionFragment;
    List<String> questionList = new ArrayList<String>();
    Spinner question_1, question_2;
    EditText answer_1, answer_2;
    Button btnRegister;

    private String mParam1;
    private String mParam2;
    private String theResult;

    private Bundle bundle;

    public QuestionFragment() {
    }

    public static QuestionFragment newInstance(String param1, String param2) {
        QuestionFragment fragment = new QuestionFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        // 添加密保问题
        questionList.add("您目前的姓名是？");
        questionList.add("您的生日是？");
        questionList.add("您的学号（或工号）是？");
        questionList.add("您母亲的生日是？");
        questionList.add("您配偶的姓名是？");
        questionList.add("对您影响最大的人名字是？");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        questionFragment = inflater.inflate(R.layout.fragment_question, container, false);
        return questionFragment;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initWidget();
        bundle = QuestionFragment.this.getArguments(); // 接收上一个 Fragment 传递的数据
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isEmpty()) {
                    // 传递数据
                    Map<String, String> registerUser = new HashMap<String, String>();
                    registerUser.put("userId", bundle.getString("phoneNum"));
                    registerUser.put("password", bundle.getString("password"));
                    registerUser.put("answer1", answer_1.getText().toString());
                    registerUser.put("answer2", answer_2.getText().toString());
                    MyThread myThread = new MyThread();
                    myThread.toParams("user/register", registerUser); // 处理数据
                    myThread.start();
                    try {
                        Thread.sleep(500); // 等待返回的结果
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if (((Double) myThread.result.get("code")).intValue() == 400) {
                        Toast.makeText(getContext(), String.valueOf(myThread.result.get("msg")), Toast.LENGTH_SHORT).show();
                    } else if (((Double) myThread.result.get("code")).intValue() == 200) {
                        Toast.makeText(getContext(), String.valueOf(myThread.result.get("msg")), Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }

    // 初始化控件
    void initWidget() {
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_selectable_list_item, questionList);
        question_1 = questionFragment.findViewById(R.id.question_1);
        question_1.setAdapter(adapter);
        question_2 = questionFragment.findViewById(R.id.question_2);
        question_2.setAdapter(adapter);
        answer_1 = questionFragment.findViewById(R.id.Answer_1);
        answer_2 = questionFragment.findViewById(R.id.Answer_2);
        btnRegister = questionFragment.findViewById(R.id.toRegister);
    }

    // 判断输入项是否有空
    boolean isEmpty() {
        if (TextUtils.isEmpty(answer_1.getText()) || TextUtils.isEmpty(answer_2.getText().toString())) {
            Toast.makeText(getContext(), "请选择问题并设置答案", Toast.LENGTH_SHORT).show();
            return true;
        }
        return false;
    }
}